package com.qf.j2205.controller;

import com.alibaba.fastjson.JSON;

import com.qf.j2205.entity.AdminInfo;
import com.qf.j2205.entity.BackUp;
import com.qf.j2205.entity.vo.BackUpVo;
import com.qf.j2205.service.BackUpService;
import com.qf.j2205.service.impl.BackUpServiceImpl;
import com.qf.j2205.util.BackUpUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName BankUpServlet
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/18 15:09
 * @Version 1.0
 **/
public class BackUpServlet extends ServletBase {

    protected void addBackup(HttpServletRequest request, HttpServletResponse response) throws IOException {

        File directory = new File("");
        String courseFile = directory.getCanonicalPath();
        Date date = new Date();
        String format = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss").format(date);
        String file = courseFile+"\\backup";
//        file+"\\"+dbName+" "+format+".sql"
        String dbName = "yulin";
        //获取ip地址
        String address = InetAddress.getLocalHost().getHostAddress();

        AdminInfo user = (AdminInfo) request.getSession().getAttribute("admin_user");
        boolean b = BackUpUtil.bankUpdate(file, dbName, format);
        if (b) {
            BackUp backUp = new BackUp(0, user.getAUsername(), date,".\\backup", dbName + " " + format, address);
            BackUpService service = new BackUpServiceImpl();
            boolean bb = service.addBackup(backUp);
            String json=null;
            if (bb){
                json = JSON.toJSONString("success");

            }else {
                String s = file + "\\" + dbName + " " + format + ".sql";
                BackUpUtil.deleteFile(s);
                json = JSON.toJSONString("fail");
            }
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println(json);
            writer.flush();
            writer.close();
        }else {
            response.sendRedirect("error.html");
        }

    }


    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {

        BackUpService service = new BackUpServiceImpl();
        List<BackUpVo> all = service.findAll();
        String json=JSON.toJSONString(all);
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.println(json);
        writer.flush();
        writer.close();
    }


    protected void deleteBackup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("bid"));
        //拼接文件路径
        File directory = new File("");
        String courseFile = directory.getCanonicalPath();
        // yulin 2022-08-18 18_40_11
        String bddate = request.getParameter("bkFile");
        // \backup
        String path = request.getParameter("path").replace(".", "");
        String filePath=courseFile+path+"\\"+bddate+".sql";
        BackUpService service = new BackUpServiceImpl();
        boolean b = service.deleteBackup(id);
        if (b) {
            System.out.println(filePath);
            BackUpUtil.deleteFile(filePath);
            String json=JSON.toJSONString("success");
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println(json);
            writer.flush();
            writer.close();
        }

    }
}
